<template>
  <div class="page_box">
    <!-- 搜索区域 开始 -->
    <el-card shadow="never">
      <el-form :inline="true" size="small" :model="formInline" class="demo-form-inline">
        <el-form-item label="名称：">
          <el-input v-model="formInline.term" placeholder="用户名/账号/手机号"></el-input>
        </el-form-item>
        <el-form-item label="注册时间：">
          <el-date-picker v-model="createTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="系统：">
          <el-select v-model="formInline.region" placeholder="活动区域">
            <el-option label="苹果" value="shanghai"></el-option>
            <el-option label="安卓" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="版本：">
          <el-select v-model="formInline.region" placeholder="活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="渠道：">
          <el-select v-model="formInline.region" placeholder="活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地区：">
          <el-select v-model="formInline.region" placeholder="活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="getList">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 搜索区域 end -->

    <div class="link_group">
      <el-button size="small" type="success" plain :loading="exportLoading" @click="downExcel">导出excel</el-button>
    </div>

    <!-- 表格 开始 -->
    <div class="table_box">
      <el-table class="tab" size="small" v-loading="loading" :data="tableData" border stripe>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="unionId" label="用户ID" show-overflow-tooltip />
        <el-table-column prop="avatar" label="头像">
          <template slot-scope="scope">
            <el-image class="avatar" fit="cover" :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]">
            </el-image>
            <!-- <img :src="scope.row.avatar" alt="" class="avatar" /> -->
          </template>
        </el-table-column>
        <el-table-column prop="username" label="用户昵称" show-overflow-tooltip />
        <el-table-column prop="registerDate" label="注册时间" show-overflow-tooltip />
        <el-table-column prop="age" label="年龄" show-overflow-tooltip />
        <el-table-column prop="gender" label="性别" show-overflow-tooltip>
          <template slot-scope="scope">{{
            scope.row.gender == 0 ? "男" : "女"
          }}</template>
        </el-table-column>
        <el-table-column prop="city" label="城市" show-overflow-tooltip />
        <el-table-column prop="appSystem" label="系统" show-overflow-tooltip />
        <el-table-column prop="appVersion" label="版本" show-overflow-tooltip />
        <el-table-column prop="name" label="IP" show-overflow-tooltip />
        <el-table-column prop="online" label="在线状态" show-overflow-tooltip>
          <template slot-scope="scope">{{
            scope.row.online ? "在线" : "离线"
          }}</template>
        </el-table-column>
        <el-table-column prop="blacklist" label="账号状态" show-overflow-tooltip>
          <template slot-scope="scope">{{
            scope.row.blacklist == 0 ? "正常" : "封禁"
          }}</template>
        </el-table-column>
        <el-table-column prop="certStatus" label="审核状态" show-overflow-tooltip>
          <template slot-scope="scope">{{
            scope.row.blacklist == 0 ? "正常" : "封禁"
          }}</template>
        </el-table-column>
        <el-table-column prop="account" label="联系方式" show-overflow-tooltip />
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-link type="primary" @click="lookDetail(scope.row)">详情</el-link>
            <el-divider direction="vertical"></el-divider>
            <el-link type="danger" @click="openDialog(scope.row)" v-if="scope.row.blacklist == 0">封禁</el-link>
            <el-link type="danger" @click="unlock(scope.row)" v-else>解封</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" hide-on-single-page :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

    <!-- 弹框 开始 -->
    <glsx-dialog :show="showDialog" title="封禁" @close="closeDialog">
      <div>
        <el-form ref="auditForm" :model="auditForm" :rules="auditFormRules" label-width="120px">
          <el-form-item label="封禁时间：" prop="handleResult">
            <el-radio-group class="el_radio_group" v-model="auditForm.handleResult">
              <!-- <el-radio :label="0">无</el-radio> -->
              <el-radio :label="1">禁言1天</el-radio>
              <el-radio :label="3">禁言3天</el-radio>
              <el-radio :label="7">禁言7天</el-radio>
              <el-radio :label="99">永久封禁</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="封禁理由：">
            <el-input type="textarea" v-model="auditForm.remark"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button type="success" plain size="small" @click="banned">确定</el-button>
        <el-button type="default" plain size="small" @click="closeDialog()">取消</el-button>
      </div>
    </glsx-dialog>
    <!-- 弹框 end -->
  </div>
</template>

<script>
import {
  GETREGISTERSEARCH,
  SAVECUSTUSERBAN,
  SAVECUSTUSERLIFTBAN,
  REGISTERLISTEXPORT,
} from "@/http/interface";
import { showModal, showToast } from "@/common/util";
import { exportExcel } from "@/common/common";
import GlsxDialog from "@/components/Dialog";
export default {
  components: {
    GlsxDialog,
  },
  data() {
    return {
      formInline: {},
      createTime: [],
      tableData: [],
      pageSize: 10,
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 30, 40],
      showDialog: false,
      loading: false,
      exportLoading: false,
      auditForm: {},
      auditFormRules: {
        handleResult: [
          { required: true, message: "请选择封禁时间", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 获取参数 */
    getParams() {
      let _this = this;
      let o = JSON.parse(JSON.stringify(_this.formInline)) || {};
      let n = _this.createTime;
      console.log(n);
      if (n && n.length > 0) {
        o.sDate = n[0];
        o.eDate = n[1];
      } else {
        delete o.sDate;
        delete o.eDate;
      }
      o.pageNumber = this.pageNumber;
      o.pageSize = this.pageSize;
      return o;
    },

    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取列表 */
    getList() {
      let _this = this;
      let oParams = this.getParams();
      _this.loading = true;
      GETREGISTERSEARCH(oParams).then((res) => {
        console.log(res);
        let obj = res.data || {};
        _this.loading = false;
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
      });
    },

    // 打开封禁弹框
    openDialog: function (row) {
      this.showDialog = true;
      this.current = row;
    },

    // 关闭弹框
    closeDialog: function () {
      this.showDialog = false;
      this.current = {};
    },

    /** 封禁 */
    banned() {
      let _this = this;
      let oParams = JSON.parse(JSON.stringify(_this.auditForm));
      oParams.userId = this.current.userId;
      _this.$refs.auditForm.validate((valid) => {
        if (!valid) {
          return false;
        }
        SAVECUSTUSERBAN(oParams).then((res) => {
          if (res.code != 200) {
            return false;
          }
          showToast({
            message: "封禁成功",
            type: "success",
          });
          _this.closeDialog();
          _this.getList();
        });
      });
    },

    // 解封
    unlock(row) {
      let _this = this;
      showModal({
        message: "确定解封该用户吗？",
        showCancelButton: true,
        success: function (done) {
          let oParams = {};
          oParams.userId = row.userId;
          SAVECUSTUSERLIFTBAN(oParams).then((res) => {
            if (res.code != 200) {
              return false;
            }
            showToast({
              message: "解封成功",
              type: "success",
            });
            _this.getList();
          });
        },
      });
    },

    /** 查看用户详情 */
    lookDetail(row) {
      this.$router.push({ name: "registerDetail", query: { id: row.userId } });
    },

    /** 导出excel */
    downExcel() {
      let _this = this;
      let options = {};
      let _obj = _this.getParams();
      let url = REGISTERLISTEXPORT;
      delete _obj.pageNumber;
      delete _obj.pageSize;
      _this.exportLoading = true;
      options.url = url;
      options.data = _obj;
      options.fileName = "注册用户列表.xlsx";
      options.method = "GET";
      options.success = function () {
        _this.exportLoading = false;
      };
      options.fail = function () {
        _this.exportLoading = false;
      };
      exportExcel(options);
    },
  },
};
</script>

<style lang="scss" scoped>
.link_group {
  margin-top: 10px;
  text-align: right;
}
.avatar {
  height: 30px;
}
</style>
